﻿@using PagedList.Mvc;

@model PagedList.IPagedList<BlogCategory>

@section styles{
    @Styles.Render("~/Content/PagedList.css")
}

@{
    string search = ViewBag.Search;
    string sortOrder = ViewBag.SortOrder;
    int pageSize = ViewBag.PageSize;
    SelectList pageSizeDDL = ViewBag.PageSizeDDL;

    int index = Model.FirstItemOnPage;
    string returnListUrl = Request.RawUrl;

    ViewBag.Title = "Danh sách danh mục blog";
}

<section class="content-header">
    <h1>
        Danh mục blog
        <small>Danh sách</small>
    </h1>
    <ol class="breadcrumb">
        <li>
            <a href="@Url.Action("Index", "Home")">
                <i class="fa fa-home"></i> Trang chủ
            </a>
        </li>
        <li class="active">Danh sách danh mục blog</li>
    </ol>
</section>
<section class="content">
    <div id="pv-alert">
        @Html.Action("_Alert", "Layout")
    </div>
    <div class="box box-solid">
        <div class="box-body">
            @using (Html.BeginForm("Index", "BlogCategory", FormMethod.Get))
            {
                @Html.Hidden("SortOrder", sortOrder)
                
                bool hasCreateBrandRole = User.IsInRole("Admin") || User.IsInRole("Create BlogCategory");

                <div class="row">
                    <div class="col-md-7">
                        @Html.DropDownList("PageSize", pageSizeDDL, new
                        {
                            @class = "form-control",
                            onchange = "this.form.submit()"
                        })

                        @if(hasCreateBrandRole)
                        {
                            <a href="@Url.Action("Create", new { returnListUrl = returnListUrl })" class="btn btn-warning">
                                <i class="fa fa-plus"></i> Tạo mới
                            </a>
                        }
                    </div>
                    <div class="col-md-5">
                        <div class="input-group">
                            @Html.TextBox("Search", search, new
                            {
                                @class = "form-control",
                                name = "search",
                                placeholder = "Tên danh mục blog"
                            })
                            <span class="input-group-btn">
                                <button type="submit" class="btn btn-primary">
                                    <i class="fa fa-search"></i>
                                </button>
                            </span>
                        </div>
                    </div>
                </div>
            }
            <div class="table-responsive">
                <table class="table table-bordered table-hover">
                    <thead>
                        <tr>
                            <th width="60">#</th>
                            <th width="110"></th>
                            <th>
                                @Html.Raw(SortOrderLink("Tên", "Name", search, pageSize))
                            </th>
                            <th width="60" class="text-right">
                                @Html.Raw(SortOrderLink("Cấp", "Level", search, pageSize))
                            </th>
                            <th width="220">
                                @Html.Raw(SortOrderLink("Tạo mới", "DateCreated", search, pageSize))
                            </th>
                            <th width="220">
                                @Html.Raw(SortOrderLink("Chỉnh sửa", "DateModified", search, pageSize))
                            </th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (BlogCategory item in Model)
                        {
                            bool hasEditBrandRole = User.IsInRole("Admin") || User.IsInRole("Edit BlogCategory");
                            bool hasDeleteBrandRole = User.IsInRole("Admin") || User.IsInRole("Delete BlogCategory");
                            
                            <tr>
                                <td>
                                    @(index++)
                                </td>
                                <td class="no-padding">
                                    @if (item.ImageUrl != null)
                                    {
                                        <img src="@item.ImageUrl?h=100" class="center-block" height="50" />
                                    }
                                    else
                                    {
                                        <img src="~/img/no-image.png?h=100" class="center-block" height="50" />
                                    }
                                </td>
                                <td>
                                    @item.Name
                                </td>
                                <td class="text-right">
                                    @item.Level
                                </td>
                                <td>
                                    <div>
                                        @item.DateCreated
                                    </div>
                                    <div>
                                        @item.CreatedBy
                                    </div>
                                </td>
                                <td>
                                    @if (item.DateModified != null)
                                    {
                                        <div>
                                            @((DateTime)item.DateModified)
                                        </div>
                                        <div>
                                            @item.ModifiedBy
                                        </div>
                                    }
                                </td>
                                <td width="110" class="control">
                                    <a href="@Url.Action("Details", new { id = item.Id, returnListUrl = returnListUrl })"
                                       data-toggle="tooltip" data-placement="top" title="Chi tiết">
                                        <i class="fa fa-info"></i>
                                    </a>
                                    @if (hasEditBrandRole)
                                    {
                                        <a href="@Url.Action("Edit", new { id = item.Id, returnListUrl = returnListUrl })"
                                           data-toggle="tooltip" data-placement="top" title="Chỉnh sửa">
                                            <i class="fa fa-edit"></i>
                                        </a>
                                    }
                                    @if (hasDeleteBrandRole)
                                    {
                                        <a href="@Url.Action("Delete", new { id = item.Id, returnListUrl = returnListUrl })"
                                           data-toggle="tooltip" data-placement="top" title="Xóa">
                                            <i class="fa fa-times"></i>
                                        </a>
                                    }
                                </td>
                            </tr>
                        }
                    </tbody>
                </table>
            </div>
            <div class="row">
                <div class="col-md-6">
                    <button type="button" class="btn btn-default" disabled="disabled">
                        @Model.FirstItemOnPage - @Model.LastItemOnPage / @Model.TotalItemCount
                    </button>
                </div>
                <div class="col-md-6 text-right">
                    @Html.PagedListPager(Model, page => Url.Action("Index", new
                    {
                        page,
                        sortOrder = sortOrder,
                        search = search,
                        pageSize = pageSize
                    }), new PagedListRenderOptions
                    {
                        LinkToFirstPageFormat = DataTableConstant.LinkToFirstPageFormat,
                        LinkToPreviousPageFormat = DataTableConstant.LinkToPreviousPageFormat,
                        LinkToNextPageFormat = DataTableConstant.LinkToNextPageFormat,
                        LinkToLastPageFormat = DataTableConstant.LinkToLastPageFormat,
                        MaximumPageNumbersToDisplay = 5
                    })
                </div>
            </div>
        </div>
    </div>
</section>

@section scripts {
    <script>
        ActiveMenu('#blogCategory-menu');
    </script>
}

@functions {
    private string SortOrderLink(string displayName, string name, string search, int pageSize)
    {
        string url = Url.Action("Index", new
        {
            sortOrder = ViewData[name + "Sort"],
            search = search,
            pageSize = pageSize
        });

        string icon = "";

        if (!String.IsNullOrWhiteSpace(ViewData[name + "Icon"] as String))
        {
            icon = ViewData[name + "Icon"] as String;
        }

        return "<a href='" + url + "'>" + displayName + icon + "</a>";
    }
}